#include "configdialog.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <qsettings.h>

using namespace Playur;

ConfigDialog::ConfigDialog(QWidget* parent)
	: QDialog(parent)
{
	QVBoxLayout* layout = new QVBoxLayout;

	QLabel* explanation = new QLabel(tr("Please enter your identifier, nickname and "
	"library path. The identifier is a unique address composed of an IRC nick "
	"and a IRC server address in the form nickname@address. The nickname is the "
	"name that will be displayed to your buddies. The library path is the folder "
	"in which your music collection is located."));
	explanation->setWordWrap(true);
	layout->addWidget(explanation);

	QGridLayout* formLayout = new QGridLayout;
	formLayout->addWidget(new QLabel(tr("Identifier:")), 0, 0);
	formLayout->addWidget(identifier = new QLineEdit, 0, 1);
	formLayout->addWidget(new QLabel(tr("Nickname:")), 1, 0);
	formLayout->addWidget(nickname = new QLineEdit, 1, 1);
	formLayout->addWidget(new QLabel(tr("Library path")), 2, 0);
	formLayout->addWidget(libraryPath = new QLineEdit, 2, 1);
	layout->addLayout(formLayout);

	QHBoxLayout* bottom = new QHBoxLayout;
	bottom->addWidget(cancelButton = new QPushButton(tr("Cancel")));
	bottom->addWidget(validateButton = new QPushButton(tr("OK")));
	layout->addLayout(bottom);

	setLayout(layout);
	setWindowTitle(tr("Configure Playur"));
	
	QSettings settings;
	identifier->setText(settings.value("identifier", tr("Your identifier here")).toString());
	nickname->setText(settings.value("nickname", tr("Happy Playur user")).toString());
	libraryPath->setText(settings.value("libraryPath").toString());
	
	connect(validateButton, SIGNAL(clicked(bool)), this, SLOT(validate()));
	connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(cancel()));
}

void ConfigDialog::validate()
{
	QSettings settings;
	settings.setValue("identifier", identifier->text());
	settings.setValue("nickname", nickname->text());
	settings.setValue("libraryPath", libraryPath->text());
	close();
}

void ConfigDialog::cancel()
{
  close();
}

